package com.rambo.androidnews.fraction.projectcase;

import com.rambo.androidnews.ListTabBean;
import com.rambo.androidnews.ResourceTable;
import com.rambo.androidnews.bean.FractionBean;
import com.rambo.androidnews.fraction.WanAdHomeFraction;
import com.rambo.androidnews.fraction.WanAdProjectFraction;
import com.rambo.androidnews.fraction.WanAdQaFraction;
import com.rambo.androidnews.fraction.WanAdWxArticleFraction;
import com.rambo.androidnews.itemprovider.WanAdMainTabProvider;
import com.rambo.androidnews.mvp.IWanAdView;
import com.rambo.androidnews.mvp.WanAdPresenter;
import com.rambo.baselib.fraction.BaseFraction;
import com.rambo.baselib.model.wanad.WanAdApiArticle;
import com.rambo.baselib.model.wanad.WanAdApiPage;
import com.rambo.baselib.model.wanad.WanAdApiResult;
import com.rambo.baselib.mvp.BasePresenter;
import com.rambo.baselib.util.FractionUtils;
import com.rambo.baselib.util.Loading;
import ohos.aafwk.ability.fraction.Fraction;
import ohos.aafwk.ability.fraction.FractionManager;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.TabList;
import ohos.app.Context;

import java.util.ArrayList;
import java.util.List;

/***********************************************************************
 * <p>@description: 玩Android 网站的项目分类tab（内部多个tab，由于嵌套多层，并且都缓存，实际中可能是会OOM）
 * <p>@author: rambo
 * <p>@version: 1.0.0
 * <p>@created on: 2022/1/13 16:07
 **********************************************************************/
public class WanAdProjectCaseFraction extends BaseFraction<IWanAdView, WanAdPresenter> implements IWanAdView {

    private TabList listTab;
    private Fraction curFraction;

    public WanAdProjectCaseFraction(Context context) {
        super(context);
    }

    @Override
    protected int getLayoutResId() {
        return ResourceTable.Layout_fraction_project_case;
    }

    @Override
    protected void initView(Component component) {
        super.initView(component);
        listTab = (TabList) component.findComponentById(ResourceTable.Id_list_tab);

    }


    @Override
    protected void registerClick() {
        super.registerClick();
        listTab.addTabSelectedListener(new TabList.TabSelectedListener() {
            @Override
            public void onSelected(TabList.Tab tab) {
                Object tag = tab.getTag();
                if (curFraction != null && tag != null && ((FractionBean) tag).fraction == curFraction) {
                    return;
                }
                FractionBean fractionBean = ((FractionBean) tag);
                if (fractionBean.fraction == null) {
                    fractionBean.fraction = new WanAdProjectFraction(context,fractionBean.id);
                }
                FractionUtils.show(getFractionAbility().getFractionManager(), ResourceTable.Id_case_container,
                        curFraction, fractionBean.fraction);
                curFraction = fractionBean.fraction;
            }

            @Override
            public void onUnselected(TabList.Tab tab) {

            }

            @Override
            public void onReselected(TabList.Tab tab) {

            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        presenter.getProjectCase();
    }

    @Override
    protected WanAdPresenter createPresenter() {
        return new WanAdPresenter();
    }

    @Override
    public void loadDataSuccess(WanAdApiResult<WanAdApiPage<List<WanAdApiArticle>>> data, String apiType) {

    }

    @Override
    public void loadDataSuccess(WanAdApiResult<List<WanAdApiArticle>> data) {
        if (data == null || data.getData() == null || data.getData().size() == 0) {
            loading.updateLoadState(Loading.LoadingState.EMPTY);
            return;
        }
        List<WanAdApiArticle> dataData = data.getData();
        for (WanAdApiArticle d : dataData) {
            TabList.Tab tab = listTab.new Tab(this);
            tab.setText(d.getName());
            tab.setTag(new FractionBean(d.getId()));
            listTab.addTab(tab);
        }
        listTab.selectTabAt(0);
    }

    @Override
    public void loadDataFailure() {
        loading.updateLoadState(Loading.LoadingState.FAIL);
    }
}
